<?php

class Controllers {

    private $_viewParams = array();
    protected $_objModule = null;
    private $_paramsByUrl = array();
    public $_rootPage = null;
    private $_actJsOnclick = null;
    protected $_objAdminModule = null;
    protected $_objValidator = null;
    private $_sessionName = null;
    protected $_objConfig = null;

    function __construct() {
        $this->setRootPage();
    }

    public function assign($key, $value) {
        $this->_viewParams[$key] = $value;
    }

    public function setHeader() {
        include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'header.php';
    }

    public function setFooter() {
        include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'footer.php';
    }

    public function setViewTemplate($folderName, $templateName) {
        extract($this->_viewParams);
        include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'header.php';
        include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $folderName . DIRECTORY_SEPARATOR . $templateName . '.php';
        include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'footer.php';
    }

    public function getViewTemplate($folderName, $templateName) {
        ob_start();
        extract($this->_viewParams);
        include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $folderName . DIRECTORY_SEPARATOR . $templateName . '.php';
        $html = ob_get_contents();
        ob_end_clean();
        return $html;
    }

    public function setParamsByUrl($aryParams) {
        $this->_paramsByUrl = $aryParams;
    }

    public function setRootPage() {
        $rootPage = 'http://' . $_SERVER['HTTP_HOST'];
        $this->_rootPage = $rootPage;
    }

    public function getRootPage() {
        return $this->_rootPage;
    }

    public function getConfig($key = null) {
        if (isset($key) && isset($this->_objConfig[$key])) {
            return $this->_objConfig[$key];
        }
        return $this->_objConfig;
    }

    public function getParamOnUrlByKey($paramKey) {
        if (isset($this->_paramsByUrl[$paramKey]) && $this->_paramsByUrl[$paramKey] != '?ajax=1') {
            return $this->_paramsByUrl[$paramKey];
        }
    }

    public function getAllPostParams() {
        return $_POST;
    }

    public function getPostParam($paramName) {
        return $_POST[$paramName];
    }

    public function getParam($key) {
        if (isset($_GET[$key])) {
            return $_GET[$key];
        }
    }

    public function setSession($key, $value) {
        $_SESSION[$key] = & $value;
    }

    public function getSession($key) {
        if ($this->hasSession($key)) {
            return $_SESSION[$key];
        }
    }

    public function clearSession($key = '') {
        if ($key == '') {
            $_SESSION = array();
            unset($_SESSION);
            @session_destroy();
        } else {
            unset($_SESSION[$key]);
        }
    }

    public function getSessionId() {
        return session_id();
    }

    public function setCookieData($key, $value, $lifeTime = 0) {
        if ($lifeTime <= 0) {
            $lifeTime = (3600 * 24 * 30); //30 day
        }
        setcookie($key, $value, time() + $lifeTime, '/', '', 0);
    }

    public function clearCookie($key) {
        setcookie($key, "", -3600);
    }

    public function getCookieData($key) {
        $value = $_COOKIE[$key];
        return $value;
    }

    public function hasCookie($key) {
        return isset($_COOKIE[$key]);
    }

    public function startSession() {
        session_cache_expire(10000);
        ini_set('session.gc_maxlifetime', 10000 * 60);
//		session_name($this->_sessionName);
        session_start();
    }

    public function hasSession($key) {
        return isset($_SESSION[$key]);
    }

    public function redirectURL($url, $delayTime = 0) {
        // prevent header injections
        $url = str_replace(array("\n", "\r"), '', $url);

        if (!headers_sent()) {
            if ((int) $delayTime < 1) {
                header("Location: {$url}");
            } else {
                header("Refresh: {$delayTime}; URL={$url}");
            }
        }

        $html = '<html>' .
                '<head>' .
                '<meta http-equiv="refresh" content="%d;url=%s"/>' .
                '</head>' .
                '</html>';

        $html = sprintf($html, $delayTime, $url);
        echo $html;
        exit;
    }

    public function getFileName($name) {
        if (isset($_FILES[$name]['name'])) {
            return $_FILES[$name]['name'];
        }
    }

    public function getTempFile($name) {
        if (isset($_FILES[$name]['tmp_name'])) {
            return $_FILES[$name]['tmp_name'];
        }
    }

    public function getFileType($name) {
        if (isset($_FILES[$name]['type'])) {
            return $_FILES[$name]['type'];
        }
    }

    public function getFileSize($name) {
        if (isset($_FILES[$name]['size'])) {
            return $_FILES[$name]['size'];
        }
    }

    public function checkFileSizeUpload($fieldName, $maxFileSize) {
        $aryFileSize = $this->getFileSize($fieldName);
        if (isset($aryFileSize[0]) && $aryFileSize[0] != '') {
            foreach ($aryFileSize as $key => $value) {
                if ($value > $maxFileSize) {
                    return false;
                }
            }
        }
        return true;
    }

    public function checkEmptyFile($fieldName) {
        $aryFile = $this->getFileName($fieldName);
        if (isset($aryFile[0]) == false || $aryFile[0] == '') {
            return true;
        }
        return false;
    }

    public function checkAllowImage($fieldName) {
        $aryFile = $this->getFileName($fieldName);
        if (isset($aryFile[0]) && $aryFile[0] != '') {
            foreach ($aryFile as $key => $fileName) {
                if ($fileName != '') {
                    if ($this->check_allow_file($fileName) == false) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public function check_allow_file($fileName) {
        $allowed_filetypes = array('.jpg', '.JPG', '.gif', '.GIF', '.jpeg', '.JPEG', '.png', '.PNG');
        $ext = substr($fileName, strpos($fileName, '.'), strlen($fileName) - 1);
        if (!in_array($ext, $allowed_filetypes)) {
            return false;
        } else {
            return true;
        }
    }

    public function resizeImage($image, $width, $height, $scale) {
        $image_data = getimagesize($image);
        $imageType = image_type_to_mime_type($image_data[2]);
        $newImageWidth = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);
        $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
        switch ($imageType) {
            case "image/gif":
                $source = imagecreatefromgif($image);
                break;
            case "image/pjpeg":
            case "image/jpeg":
            case "image/jpg":
                $source = imagecreatefromjpeg($image);
                break;
            case "image/png":
            case "image/x-png":
                $source = imagecreatefrompng($image);
                break;
        }
        imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);

        switch ($imageType) {
            case "image/gif":
                imagegif($newImage, $image);
                break;
            case "image/pjpeg":
            case "image/jpeg":
            case "image/jpg":
                imagejpeg($newImage, $image, 90);
                break;
            case "image/png":
            case "image/x-png":
                imagepng($newImage, $image);
                break;
        }

        return $image;
    }

//You do not need to alter these functions
    public function getHeight($image) {
        $size = getimagesize($image);
        $height = $size[1];
        return $height;
    }

//You do not need to alter these functions
    public function getWidth($image) {
        $size = getimagesize($image);
        $width = $size[0];
        return $width;
    }

    public function multiUploadFile($fieldName, $dirUpload, &$resultFileUpLoaded, $max_width = null) {
        $intIsOk = 1;
        $resultFileUpLoaded = array();
        //Get array from $fileName
        $aryFile = $this->getFileName($fieldName);
        $aryTempFile = $this->getTempFile($fieldName);
        $aryFileType = $this->getFileType($fieldName);
        $aryFileSize = $this->getFileSize($fieldName);
        // Check url
        if (substr($dirUpload, -1, 1) != DIRECTORY_SEPARATOR)
            $dirUpload .= DIRECTORY_SEPARATOR;
        @mkdir($dirUpload, 0777, true);
        // Check file
        if (isset($aryFile[0]) && $aryFile[0] != '') {
            foreach ($aryFile as $key => $fileName) {
                // Check isUpload
                if (is_uploaded_file($aryTempFile[$key]) && is_file($aryTempFile[$key])) {
                    $imageName = microtime() . $fileName;
                    $replace = array(' ', '~', '!', '$', '%', '^', '(', ')', ';', '\'', ',');
                    $imageName = str_replace($replace, '_', $imageName);
                    $aryFileUpload = array();
                    $aryFileUpload['image_name'] = $imageName;
                    $resultFileUpLoaded[] = $aryFileUpload;
                    $imageLocation = $dirUpload . $imageName;
                    @copy($aryTempFile[$key], $imageLocation);
                    if (isset($max_width) && is_numeric($max_width)) {
                        $width = $this->getWidth($imageLocation);
                        $height = $this->getHeight($imageLocation);
                        //Scale the image if it is greater than the width set above
                        if ($width > $max_width) {
                            if ($height < $max_width) {
                                $scale = $max_width / $width;
                                $uploaded = $this->resizeImage($imageLocation, $width, $height, $scale);
                            } else if ($height > $max_width) {
                                if ($height > $width) {
                                    $scale = $max_width / $height;
                                    $uploaded = $this->resizeImage($imageLocation, $width, $height, $scale);
                                } else {
                                    $scale = $max_width / $width;
                                    $uploaded = $this->resizeImage($imageLocation, $width, $height, $scale);
                                }
                            }
                        } else {
                            if ($height < $max_width) {
                                $scale = 1;
                                $uploaded = $this->resizeImage($imageLocation, $width, $height, $scale);
                            } else {
                                $scale = $max_width / $height;
                                $uploaded = $this->resizeImage($imageLocation, $width, $height, $scale);
                            }
                        }
                    }
                }
            }
        }
        return $intIsOk;
    }

    public function setActionJSPaging($actJs) {
        $this->_actJsOnclick = $actJs;
    }

    public function AjaxPagination($aryPaging) {

        /* Setup page vars for display. */
        $adjacents = 3;
        $page = $aryPaging['current_page'];
        $prev = $aryPaging['current_page'] - 1;       //previous page is page - 1
        $next = $aryPaging['current_page'] + 1;       //next page is page + 1
        $lastpage = $aryPaging['total_page'];  //lastpage is = total pages / items per page, rounded up.
        $lpm1 = $lastpage - 1;      //last page minus 1

        /*
          Now we apply our rules and draw the pagination object.
          We're actually saving the code to a variable in case we want to draw it more than once.
         */
        $pagination = "";
        if ($lastpage > 1) {
            $pagination .= "<div class=\"pagination\">";
            //previous button
            if ($page > 1)
                $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$prev')>« Trang trước</a>";
            else
                $pagination.= "<span class=\"disabled\">« Trang trước</span>";

            //pages
            if ($lastpage < 7 + ($adjacents * 2)) { //not enough pages to bother breaking it up
                for ($counter = 1; $counter <= $lastpage; $counter++) {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$counter')>$counter</a>";
                }
            }
            elseif ($lastpage > 5 + ($adjacents * 2)) { //enough pages to hide some
                //close to beginning; only hide later pages
                if ($page < 1 + ($adjacents * 2)) {
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$counter')>$counter</a>";
                    }
                    $pagination.= "...";
                    $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$lpm1')>$lpm1</a>";
                    $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$lastpage')>$lastpage</a>";
                }
                //in middle; hide some front and some back
                elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
                    $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'1')>1</a>";
                    $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'2')>2</a>";
                    $pagination.= "...";
                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a onclick=$this->_actJsOnclick'$counter')>$counter</a>";
                    }
                    $pagination.= "...";
                    $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$lpm1')>$lpm1</a>";
                    $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$lastpage')>$lastpage</a>";
                }
                //close to end; only hide early pages
                else {
                    $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'1')>1</a>";
                    $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'2')>2</a>";
                    $pagination.= "...";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$counter')>$counter</a>";
                    }
                }
            }

            //next button
            if ($page < $counter - 1)
                $pagination.= "<a class=\"cpointer\" onclick=$this->_actJsOnclick'$next')>Trang sau »</a>";
            else
                $pagination.= "<span class=\"disabled\">Trang sau »</span>";
            $pagination.= "</div>\n";
        }

        return $pagination;
    }

    public function pagination($link, $aryPaging, $showHtml = false) {

        if ($showHtml == false) {
            $html = '';
        } else {
            $html = '.html';
        }
        /* Setup page vars for display. */
        $adjacents = 3;
        $page = $aryPaging['current_page'];
        $prev = $aryPaging['current_page'] - 1;       //previous page is page - 1
        $next = $aryPaging['current_page'] + 1;       //next page is page + 1
        $lastpage = $aryPaging['total_page'];  //lastpage is = total pages / items per page, rounded up.
        $lpm1 = $lastpage - 1;      //last page minus 1

        /*
          Now we apply our rules and draw the pagination object.
          We're actually saving the code to a variable in case we want to draw it more than once.
         */
        $pagination = "";
        if ($lastpage > 1) {
            $pagination .= "<div class=\"pagination\">";
            //previous button
            if ($page > 1)
                $pagination.= "<a href=\"$link" . "$prev$html\">« Trang trước</a>";
            else
                $pagination.= "<span class=\"disabled\">« Trang trước</span>";

            //pages
            if ($lastpage < 7 + ($adjacents * 2)) { //not enough pages to bother breaking it up
                for ($counter = 1; $counter <= $lastpage; $counter++) {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$link" . "$counter$html\">$counter</a>";
                }
            }
            elseif ($lastpage > 5 + ($adjacents * 2)) { //enough pages to hide some
                //close to beginning; only hide later pages
                if ($page < 1 + ($adjacents * 2)) {
                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a href=\"$link" . "$counter$html\">$counter</a>";
                    }
                    $pagination.= "...";
                    $pagination.= "<a href=\"$link" . "$lpm1$html\">$lpm1</a>";
                    $pagination.= "<a href=\"$link" . "$lastpage$html\">$lastpage</a>";
                }
                //in middle; hide some front and some back
                elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
                    $pagination.= "<a href=\"$link" . "1$html\">1</a>";
                    $pagination.= "<a href=\"$link" . "2$html\">2</a>";
                    $pagination.= "...";
                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a href=\"$link" . "$counter$html\">$counter</a>";
                    }
                    $pagination.= "...";
                    $pagination.= "<a href=\"$link" . "$lpm1$html\">$lpm1</a>";
                    $pagination.= "<a href=\"$link" . "$lastpage$html\">$lastpage</a>";
                }
                //close to end; only hide early pages
                else {
                    $pagination.= "<a href=\"$link" . "1$html\">1</a>";
                    $pagination.= "<a href=\"$link" . "2$html\">2</a>";
                    $pagination.= "...";
                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a href=\"$link" . "$counter$html\">$counter</a>";
                    }
                }
            }

            //next button
            if ($page < $counter - 1)
                $pagination.= "<a href=\"$link" . "$next$html\">Trang sau » </a>";
            else
                $pagination.= "<span class=\"disabled\">Trang sau »</span>";
            $pagination.= "</div>\n";
        }

        return $pagination;
    }

    public function filterUnicode($str) {
        $marTViet = array("à", "á", "ạ", "ả", "ã", "â", "ầ", "ấ", "ậ", "ẩ", "ẫ", "ă",
            "ằ", "ắ", "ặ", "ẳ", "ẵ", "è", "é", "ẹ", "ẻ", "ẽ", "ê", "ề"
            , "ế", "ệ", "ể", "ễ",
            "ì", "í", "ị", "ỉ", "ĩ",
            "ò", "ó", "ọ", "ỏ", "õ", "ô", "ồ", "ố", "ộ", "ổ", "ỗ", "ơ"
            , "ờ", "ớ", "ợ", "ở", "ỡ",
            "ù", "ú", "ụ", "ủ", "ũ", "ư", "ừ", "ứ", "ự", "ử", "ữ",
            "ỳ", "ý", "ỵ", "ỷ", "ỹ",
            "đ",
            "À", "Á", "Ạ", "Ả", "Ã", "Â", "Ầ", "Ấ", "Ậ", "Ẩ", "Ẫ", "Ă"
            , "Ằ", "Ắ", "Ặ", "Ẳ", "Ẵ",
            "È", "É", "Ẹ", "Ẻ", "Ẽ", "Ê", "Ề", "Ế", "Ệ", "Ể", "Ễ",
            "Ì", "Í", "Ị", "Ỉ", "Ĩ",
            "Ò", "Ó", "Ọ", "Ỏ", "Õ", "Ô", "Ồ", "Ố", "Ộ", "Ổ", "Ỗ", "Ơ"
            , "Ờ", "Ớ", "Ợ", "Ở", "Ỡ",
            "Ù", "Ú", "Ụ", "Ủ", "Ũ", "Ư", "Ừ", "Ứ", "Ự", "Ử", "Ữ",
            "Ỳ", "Ý", "Ỵ", "Ỷ", "Ỹ",
            "Đ");

        $marKoDau = array("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"
            , "a", "a", "a", "a", "a", "a",
            "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e",
            "i", "i", "i", "i", "i",
            "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o"
            , "o", "o", "o", "o", "o",
            "u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u",
            "y", "y", "y", "y", "y",
            "d",
            "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a"
            , "a", "a", "a", "a", "a",
            "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e",
            "i", "i", "i", "i", "i",
            "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o"
            , "o", "o", "o", "o", "o",
            "u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u",
            "y", "y", "y", "y", "y",
            "d");
        $str = str_replace($marTViet, $marKoDau, $str);

        $str = str_replace(' ', '-', trim(str_replace(' +', ' ', preg_replace('/[^a-zA-Z0-9@\s]/', '', strtolower($str)))));

        return $str;
    }

    public function encodingUnicode($str) {
        $marTViet = array("à", "á", "ạ", "ả", "ã", "â", "ầ", "ấ", "ậ", "ẩ", "ẫ", "ă",
            "ằ", "ắ", "ặ", "ẳ", "ẵ",
            "è", "é", "ẹ", "ẻ", "ẽ", "ê", "ề", "ế", "ệ", "ể", "ễ",
            "ì", "í", "ị", "ỉ", "ĩ",
            "ò", "ó", "ọ", "ỏ", "õ", "ô", "ồ", "ố", "ộ", "ổ", "ỗ", "ơ"
            , "ờ", "ớ", "ợ", "ở", "ỡ",
            "ù", "ú", "ụ", "ủ", "ũ", "ư", "ừ", "ứ", "ự", "ử", "ữ",
            "ỳ", "ý", "ỵ", "ỷ", "ỹ",
            "đ",
            "À", "Á", "Ạ", "Ả", "Ã", "Â", "Ầ", "Ấ", "Ậ", "Ẩ", "Ẫ", "Ă"
            , "Ằ", "Ắ", "Ặ", "Ẳ", "Ẵ",
            "È", "É", "Ẹ", "Ẻ", "Ẽ", "Ê", "Ề", "Ế", "Ệ", "Ể", "Ễ",
            "Ì", "Í", "Ị", "Ỉ", "Ĩ",
            "Ò", "Ó", "Ọ", "Ỏ", "Õ", "Ô", "Ồ", "Ố", "Ộ", "Ổ", "Ỗ", "Ơ"
            , "Ờ", "Ớ", "Ợ", "Ở", "Ỡ",
            "Ù", "Ú", "Ụ", "Ủ", "Ũ", "Ư", "Ừ", "Ứ", "Ự", "Ử", "Ữ",
            "Ỳ", "Ý", "Ỵ", "Ỷ", "Ỹ",
            "Đ");

        $marKoDau = array("a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11"
            , "a12", "a13", "a14", "a15", "a16", "a17",
            "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10", "e11",
            "i1", "i2", "i3", "i4", "i5",
            "o1", "o2", "o3", "o4", "o5", "o6", "o7", "o8", "o9", "o10", "o11", "o12"
            , "o13", "o14", "o15", "o16", "o17",
            "u1", "u2", "u3", "u4", "u5", "u6", "u7", "u8", "u9", "u10", "u11",
            "y1", "y2", "y3", "y4", "y5",
            "d1",
            "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12"
            , "a13", "a14", "a15", "a16", "a17",
            "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10", "e11",
            "i1", "i2", "i3", "i4", "i5",
            "o1", "o2", "o3", "o4", "o5", "o6", "o7", "o8", "o9", "o10", "o11", "o12"
            , "o13", "o14", "o15", "o16", "o17",
            "u1", "u2", "u3", "u4", "u5", "u6", "u7", "u8", "u9", "u10", "u11",
            "y1", "y2", "y3", "y4", "y5",
            "d1");
        $str = str_replace($marTViet, $marKoDau, $str);

        $str = str_replace(' ', '-', trim(str_replace(' +', ' ', preg_replace('/[^a-zA-Z0-9@\s]/', '', strtolower($str)))));

        return $str;
    }

    public function removeHTML($str) {
        return preg_replace('/<([\/\w]+)[^>]*>/si', '', $str);
    }

    public function removeSearhKeyword($str) {
        $str = mb_strtolower($str, 'utf-8');

//        $str = strip_tags($str);

        $str = trim(preg_replace('/[\@\^\,\*\?\(\)\{\}\=\!\<\>\|\%\#\&\$\'\"\;\:\-\_\+\/]+/', '', $str));

        return $str;
    }

    public function cutString($str, $n=500) {

        $str = $this->removeHTML($str);

        $str = addslashes($str);

        if (strlen($str) < $n)
            return $str;

        $html = substr($str, 0, $n);

        $html = substr($html, 0, strrpos($html, ' '));

        return $html . '...';
    }

}

?>
